//
// SoapTrace.Net
//
// Copyright (C) 2004-2007 Matthew Ward
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Matthew Ward (mrward@users.sourceforge.net)

using SoapTrace.Core.Diagnostics;
using System;
using System.Net.Sockets;
using System.Text;
using WinPcap;

namespace SoapTrace.Core.Communication
{
	/// <summary>
	/// Represents a connection to the remote server.  Used by
	/// the <see cref="SoapMessageProxyServer"/> class.
	/// </summary>
	public class ServerConnection : TcpSocketConnection
	{
		/// <summary>
		/// Creates a new instance of the <see cref="ServerConnection"/>
		/// class.
		/// </summary>
		public ServerConnection( Socket socket )
			: base( socket )
		{
		}
		
		/// <summary>
		/// Creates a new instance of the <see cref="ServerConnection"/>
		/// class.
		/// </summary>
		/// <param name="address">The ip address of the server.</param>
		/// <param name="port">The server's port.</param>
		public ServerConnection( string address, int port )
			: base( address, port )
		{
		}
		
		/// <summary>
		/// Bytes received from the client.
		/// </summary>
		/// <param name="bytes">A byte array.</param>
		/// <param name="length">The length of the byte array contain
		/// bytes.</param>
		protected override void BytesReceived( byte[ ] bytes, int length )
		{
			if( Logger.IsLevelEnabled( LogLevel.Debug ) )
			{
				string BytesAsString = String.Empty;
				
				if( Logger.LogBytesAsString )
				{
					BytesAsString = ServerConnection.GetBytesAsString( bytes, length );
				}
				else
				{
					BytesAsString = "\r\n" + Packet.GetRawHex( bytes, length );
				}

				Logger.LogDebug( String.Concat( "Server.BytesRecv: ", BytesAsString ) );
			}
		}

		/// <summary>
		/// Bytes sent to the client.
		/// </summary>
		/// <param name="bytes">A byte array.</param>
		/// <param name="length">The length of the byte array contain
		/// bytes.</param>
		protected override void BytesSent( byte[ ] bytes, int length )
		{
			if( Logger.IsLevelEnabled( LogLevel.Debug ) )
			{
				string BytesAsString = String.Empty;
				
				if( Logger.LogBytesAsString )
				{
					BytesAsString = ServerConnection.GetBytesAsString( bytes, length );
				}
				else
				{
					BytesAsString = "\r\n" + Packet.GetRawHex( bytes, length );
				}

				Logger.LogDebug( String.Concat( "Server.BytesSent: ", BytesAsString ) );
			}		
		}
		
		/// <summary>
		/// Gets a byte array as a formatted string.
		/// </summary>
		/// <param name="bytes">The byte array.</param>
		/// <param name="length">The length of the byte array.</param>
		/// <returns>A formatted string.</returns>
		private static string GetBytesAsString( byte[ ] bytes, int length )
		{
			string FormattedText = UnicodeEncoding.UTF8.GetString( bytes, 0, length );
			
			FormattedText = FormattedText.Replace( "\n", "\\n" );
			FormattedText = FormattedText.Replace( "\r", "\\r" );
			
			return FormattedText;
		}
	}
}
